//
//  TNewsViewController.m
//  LoveShare
//
//  Created by ways on 2017/4/20.
//  Copyright © 2017年 tikeyc. All rights reserved.
//

#import "TNewsViewController.h"

#import "SPKitExample.h"
#import "SPUtil.h"
#import <YWExtensionForCustomerServiceFMWK/YWExtensionForCustomerServiceFMWK.h>
#import "SPContactListController.h"
#import "SPTribeListViewController.h"

@interface TNewsViewController ()

@property (nonatomic,strong) YWConversationListViewController *conversationListController;

@end

@implementation TNewsViewController

- (void)dealloc
{
    NSLog(@"%@ 成功销毁了，无内存泄漏",self);
}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    [self creatIMVC];
    self = [super initWithRootViewController:_conversationListController];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



#pragma mark - 消息中心

- (void)creatIMVC {
//    if (_conversationListController != nil) {
//        UINavigationController *naviController = [[UINavigationController alloc] initWithRootViewController:_conversationListController];
//        [self presentViewController:naviController animated:YES completion:^{
//            
//        }];
//        //[self.viewController.navigationController pushViewController:_conversationListController animated:YES];
//        return;
//    }
    /// 会话列表页面
    _conversationListController = [[SPKitExample sharedInstance].ywIMKit makeConversationListViewController];
    [[SPKitExample sharedInstance] exampleCustomizeConversationCellWithConversationListController:_conversationListController];
    
    __weak __typeof(_conversationListController) weakConversationListController = _conversationListController;
    _conversationListController.didSelectItemBlock = ^(YWConversation *aConversation) {
        if ([aConversation isKindOfClass:[YWCustomConversation class]]) {
            YWCustomConversation *customConversation = (YWCustomConversation *)aConversation;
            [customConversation markConversationAsRead];
            
            if ([customConversation.conversationId isEqualToString:SPTribeSystemConversationID]) {
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Tribe" bundle:nil];
                UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"SPTribeSystemConversationViewController"];
                [weakConversationListController.navigationController pushViewController:controller animated:YES];
            }
            else if ([customConversation.conversationId isEqualToString:kSPCustomConversationIdForFAQ]) {
                YWWebViewController *controller = [YWWebViewController makeControllerWithUrlString:@"https://bbs.aliyun.com/searcher.php?step=2&method=AND&type=thread&verify=d26d3c6e63c0b37d&sch_area=1&fid=285&sch_time=all&keyword=汇总" andImkit:[SPKitExample sharedInstance].ywIMKit];
                [controller setHidesBottomBarWhenPushed:YES];
                [controller setTitle:@"云旺iOS精华问题"];
                [weakConversationListController.navigationController pushViewController:controller animated:YES];
            }
            else {
                YWWebViewController *controller = [YWWebViewController makeControllerWithUrlString:@"https://im.taobao.com/" andImkit:[SPKitExample sharedInstance].ywIMKit];
                [controller setHidesBottomBarWhenPushed:YES];
                [controller setTitle:@"功能介绍"];
                [weakConversationListController.navigationController pushViewController:controller animated:YES];
            }
        }
        else {
//            NSLog(@"conversationId:%@",aConversation.conversationId);
            [[SPKitExample sharedInstance] exampleOpenConversationViewControllerWithConversation:aConversation
                                                                        fromNavigationController:weakConversationListController.navigationController];
        }
    };
    
    _conversationListController.didDeleteItemBlock = ^ (YWConversation *aConversation) {
        if ([aConversation.conversationId isEqualToString:SPTribeSystemConversationID]) {
            [[[SPKitExample sharedInstance].ywIMKit.IMCore getConversationService] removeConversationByConversationId:[SPKitExample sharedInstance].tribeSystemConversation.conversationId error:NULL];
        }
    };
    
    _conversationListController.ywcsTrackTitle = @"会话列表";
    
    // 会话列表空视图
    if (_conversationListController)
    {
        CGRect frame = CGRectMake(0, 0, 100, 100);
        UIView *viewForNoData = [[UIView alloc] initWithFrame:frame];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_logo"]];
        imageView.center = CGPointMake(viewForNoData.frame.size.width/2, viewForNoData.frame.size.height/2);
        [imageView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin];
        
        [viewForNoData addSubview:imageView];
        
        _conversationListController.viewForNoData = viewForNoData;
    }
    
    {
        __weak typeof(_conversationListController) weakController = _conversationListController;
        @weakify(self)
        [_conversationListController setViewDidLoadBlock:^{
            @strongify(self)
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
            button.frame = CGRectMake(0, 0, 44, 44);
            [button setImage:[UIImage imageNamed:@"common_back_barItem_button"] forState:UIControlStateNormal];
            [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
                [weakController dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }];
            //
            weakController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
            
            //
            UIView *rightItemView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
            NSArray *titles = @[@"联系人",@"群列表"];
            for (int i = 0; i < titles.count; i++) {
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
                button.frame = CGRectMake(60*i, 0, 60, 44);
                [button setTitle:titles[i] forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                
                [button addTarget:self action:@selector(rightBarButtonItemClickAction:) forControlEvents:UIControlEventTouchUpInside];
                [rightItemView addSubview:button];
            }
            weakController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItemView];
            
            
            if ([weakController respondsToSelector:@selector(traitCollection)]) {
                UITraitCollection *traitCollection = weakController.traitCollection;
                if ( [traitCollection respondsToSelector:@selector(forceTouchCapability)] ) {
                    if (traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
                        [weakController registerForPreviewingWithDelegate:weakController sourceView:weakController.tableView];
                    }
                }
            }
        }];
    }
    
    //    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
//    UINavigationController *naviController = [[UINavigationController alloc] initWithRootViewController:_conversationListController];
//    [self presentViewController:naviController animated:YES completion:^{
//        
//    }];
    
    @weakify(self)
    [[SPKitExample sharedInstance].ywIMKit setUnreadCountChangedBlock:^(NSInteger aCount) {
        @strongify(self)
        NSString *badgeValue = aCount > 0 ?[ @(aCount) stringValue] : nil;
        self.presentingViewController.navigationController.tabBarItem.badgeValue = badgeValue;
    }];
    
    
}

#pragma mark -

- (void)rightBarButtonItemClickAction:(UIButton *)button {
    if (button.tag == 0) {
        [self gotoContactVC];
    } else {
        [self gotoTribeVC];
    }
}

- (void)gotoContactVC {
    SPContactListController *contactList = [[SPContactListController alloc] init];
    [self pushViewController:contactList animated:YES];
}

- (void)gotoTribeVC {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Tribe" bundle:nil];
    SPTribeListViewController *tribeList = [storyboard instantiateInitialViewController];
    [self pushViewController:tribeList animated:YES];
}


@end
